package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.example.demo.domain.User;
import com.example.demo.service.ShardingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @Date 2023/10/26 11:01
 */
@RestController
@RequestMapping("/sharding")
public class ShardingController {

    @Autowired
    ShardingService service;

    @RequestMapping("/query")
    public String test(){
        return JSON.toJSONString(service.query());
    }

    @RequestMapping("/insert")
    public String isnert(long  id){
        service.insert(id);
        return "OK";
    }


    @RequestMapping("/insert2")
    public String isnert2(){
//        service.inserts();
        Random rand = new Random();
        int randomNumber = rand.nextInt(1000);
        long num = Long.valueOf(randomNumber);

        List<User> users = new ArrayList<>();
        for (int i = 0; i <10; i++) {
            User build = User.builder().id(num + i).age(18).name("慈" + i).build();
            users.add(build);
        }
        // mybatis-plus 批量插入 ，不写sql版
        service.saveBatch(users);
        return "OK";
    }


    @RequestMapping("/insert3")
    public String isner3(){
        service.inserts();
        return "OK";
    }
}
